package cn.ld.module.system.controller.admin.sms;

import cn.ld.framework.common.pojo.CommonResult;
import cn.ld.framework.common.pojo.PageResult;
import cn.ld.framework.common.util.object.BeanUtils;
import cn.ld.module.system.controller.admin.sms.vo.channel.SmsChannelPageInVO;
import cn.ld.module.system.controller.admin.sms.vo.channel.SmsChannelOutVO;
import cn.ld.module.system.controller.admin.sms.vo.channel.SmsChannelSaveInVO;
import cn.ld.module.system.controller.admin.sms.vo.channel.SmsChannelSimpleOutVO;
import cn.ld.module.system.dal.entity.sms.SmsChannelEntity;
import cn.ld.module.system.service.sms.SmsChannelService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import jakarta.annotation.Resource;
import jakarta.validation.Valid;
import java.util.Comparator;
import java.util.List;

import static cn.ld.framework.common.pojo.CommonResult.success;

@Tag(name = "管理后台 - 短信渠道")
@RestController
@RequestMapping("system/sms-channel")
public class SmsChannelController {

    @Resource
    private SmsChannelService smsChannelService;

    @PostMapping("/create")
    @Operation(summary = "创建短信渠道")
    @PreAuthorize("@ss.hasPermission('system:sms-channel:create')")
    public CommonResult<Long> createSmsChannel(@Valid @RequestBody SmsChannelSaveInVO createInVO) {
        return success(smsChannelService.createSmsChannel(createInVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新短信渠道")
    @PreAuthorize("@ss.hasPermission('system:sms-channel:update')")
    public CommonResult<Boolean> updateSmsChannel(@Valid @RequestBody SmsChannelSaveInVO updateInVO) {
        smsChannelService.updateSmsChannel(updateInVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除短信渠道")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('system:sms-channel:delete')")
    public CommonResult<Boolean> deleteSmsChannel(@RequestParam("id") Long id) {
        smsChannelService.deleteSmsChannel(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得短信渠道")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('system:sms-channel:query')")
    public CommonResult<SmsChannelOutVO> getSmsChannel(@RequestParam("id") Long id) {
        SmsChannelEntity channel = smsChannelService.getSmsChannel(id);
        return success(BeanUtils.toBean(channel, SmsChannelOutVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得短信渠道分页")
    @PreAuthorize("@ss.hasPermission('system:sms-channel:query')")
    public CommonResult<PageResult<SmsChannelOutVO>> getSmsChannelPage(@Valid SmsChannelPageInVO pageVO) {
        PageResult<SmsChannelEntity> pageResult = smsChannelService.getSmsChannelPage(pageVO);
        return success(BeanUtils.toBean(pageResult, SmsChannelOutVO.class));
    }

    @GetMapping({"/list-all-simple", "/simple-list"})
    @Operation(summary = "获得短信渠道精简列表", description = "包含被禁用的短信渠道")
    public CommonResult<List<SmsChannelSimpleOutVO>> getSimpleSmsChannelList() {
        List<SmsChannelEntity> list = smsChannelService.getSmsChannelList();
        list.sort(Comparator.comparing(SmsChannelEntity::getId));
        return success(BeanUtils.toBean(list, SmsChannelSimpleOutVO.class));
    }

}
